<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport('#exportTempSom', '超发订单数据')"
          v-hasPermi="['marketing:tempSom:export']"
        >
          详细数据导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport('#exportTempSomWldm', '超发订单数据-按物料统计')"
          v-hasPermi="['marketing:tempSom:export']"
        >
          物料统计导出
        </el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-tabs v-model="activeTab">
      <el-tab-pane label="详细数据" name="first">
        <el-table
          @scrollBottom="load1"
          height="600px"
          highlight-current-row
          border
          stripe
          v-loading="loading"
          :data="tempSomList"
          @selection-change="handleSelectionChange"
          id="exportTempSom"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column
            label="超发单号"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="tsCfdh"
          />
          <el-table-column
            label="超发营销单号"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="tsOldDjbh"
          />
          <el-table-column
            label="接收营销单号"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="tsNewDjbh"
          />
          <el-table-column
            label="客户编码"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="itmCustwldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="250"
            align="center"
            prop="itmPmgg"
          />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="tsWldm"
          />
          <el-table-column
            label="出货数量"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="tsChsl"
          />
          <el-table-column
            label="建立人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="tsJlry"
          />
          <el-table-column
            label="建立日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="tsJlrq"
            width="180"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.tsJlrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="更改人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="tsGgry"
          />
          <el-table-column
            label="更改日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="tsGgrq"
            width="180"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.tsGgrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按物料统计" name="second">
        <el-table
          @scrollBottom="load2"
          height="600px"
          highlight-current-row
          border
          stripe
          v-loading="loading"
          :data="tempSomWldmList"
          @selection-change="handleSelectionChange"
          id="exportTempSomWldm"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="tsWldm"
          />
          <el-table-column
            label="客户编码"
            :show-overflow-tooltip="true"
            width="250"
            align="center"
            prop="itmCustwldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="250"
            align="center"
            prop="itmPmgg"
          />
          <el-table-column
            label="超发数量"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="tsChsl"
          />
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="98px"
      >
        <el-col :span="12">
          <el-form-item label="建立日期">
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户编码" prop="itmCustwldm">
            <el-input
              v-model="queryParams.itmCustwldm"
              placeholder="请输入客户编码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="超发营销单号" prop="tsOldDjbh">
            <el-input
              v-model="queryParams.tsOldDjbh"
              placeholder="请输入超发营销单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="接收营销单号" prop="tsNewDjbh">
            <el-input
              v-model="queryParams.tsNewDjbh"
              placeholder="请输入接收营销单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <RadioGroup
            v-model="queryParams.tsZtbz"
            style="width: 280px; margin-left: 68px"
          >
            <Radio label="1" style="margin: 0px 30px">未接收</Radio>
            <Radio label="2" style="margin-right: 30px">已接收</Radio>
            <Radio label="0" align="right">全部</Radio>
          </RadioGroup>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改临时营销单记录对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      ></el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTempSomReport } from "@/api/marketing/som";
import { exportTable } from "@/utils/ruoyi";

export default {
  name: "TempSom",
  data() {
    return {
      // 遮罩层
      loading: true,
      tableH: document.body.clientHeight - 230,
      activeTab: "first",
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total1: 0,
      total2: 0,
      // 临时营销单记录表格数据
      tempSomList: [],
      tempSomWldmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        tsChdDjbh: null,
        tsOldDjbh: null,
        tsNewDjbh: null,
        itmCustwldm: null,
        tsJlrq: null,
        tsZtbz: "1",
      },
      dateRange: [
        new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 60),
        new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 60),
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询临时营销单记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.params["beginDate"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endDate"] =
          this.moment(this.dateRange[1]).format("YYYY-MM-DD ") + "23:59:59";
      }
      this.queryParams.pageNum = 1;
      listTempSomReport(this.queryParams).then((response) => {
        this.tempSomList = [];
        this.tempSomWldmList = [];
        this.tempSomList = this.tempSomList.concat(response.data.list1.rows);
        this.tempSomWldmList = this.tempSomWldmList.concat(
          response.data.list2.rows
        );
        this.total1 = response.data.list1.total;
        this.total2 = response.data.list2.total;
        this.loading = false;
      });
    },

    //load
    load1() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total1 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listTempSomReport(this.queryParams).then((response) => {
          this.tempSomList = this.tempSomList.concat(response.data.list1.rows);
          this.tempSomWldmList = this.tempSomWldmList.concat(
            response.data.list2.rows
          );
          this.total1 = response.data.list1.total;
          this.total2 = response.data.list2.total;
          this.loading = false;
        });
      }
      return 0;
    },
    load2() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total2 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listTempSomReport(this.queryParams).then((response) => {
          this.tempSomList = this.tempSomList.concat(response.data.list1.rows);
          this.tempSomWldmList = this.tempSomWldmList.concat(
            response.data.list2.rows
          );
          this.total1 = response.data.list1.total;
          this.total2 = response.data.list2.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tsId: null,
        tsChdDjbh: null,
        tsOldDjbh: null,
        tsNewDjbh: null,
        tsWldm: null,
        tsChsl: null,
        tsJlry: null,
        tsJlrq: null,
        tsGgry: null,
        tsGgrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.tempSomList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.tsId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加临时营销单记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const tsId = row.tsId || this.ids;
      getTempSom(tsId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改临时营销单记录";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询临时营销单记录";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.tsId != null) {
            updateTempSom(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTempSom(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tsIds = row.tsId || this.ids;
      this.$confirm(
        '是否确认删除临时营销单记录编号为"' + tsIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delTempSom(tsIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport(a, b) {
      exportTable(a, b);
    },
  },
};
</script>

<style lang="scss" scoped></style>
